#########################################################

#                                                       #

#Head Tracker Proof of Concept                          #

#D Burrin                                               #

#24/6/16                                                #

#                                                       #

#########################################################

from microbit import *

import random

#initilise the serial port

uart.init(baudrate = 9600, bits=8,parity=uart.EVEN,stop=1)





while True:

    if button_a.is_pressed():

        while True:

            #read values for acceleromter and compass heading

            x=accelerometer.get_x()

            y=accelerometer.get_y()

            

            #set click of

            click="0"

            

            ##rough scaling - needs a propper A2D review

            x=x+1024

            x=str(x)

            #pass with 0 and cut to ensure 4 bits read for x

            x="0000000"+x

            x=x[-4:]

            #rough scaling

            y=y+250

            y=str(y)

            #pack with 0 and cut to ensure 4 bits for y

            y="0000000"+y

            y=y[-4:]

            

            #check for button press

            if button_a.is_pressed():

                click="1"

                sleep(50)

            

            #4 bits for x for bits for y + 1 bit for left click

            xy=x+y+click

            #write string

            uart.write(xy)

            #needs some time to ensure right is complete

            sleep(100)

                    

            if button_b.is_pressed():

                break        

        

    else:

        display.show("PRESS A")

